package com.gitee.qdbp.general.system.biz.personnel.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.base.system.model.CreatorIdAware;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.general.system.api.personnel.basic.IUserCoreBasic;
import com.gitee.qdbp.general.system.api.personnel.model.UserCoreBean;
import com.gitee.qdbp.general.system.api.personnel.model.UserCoreWhere;
import com.gitee.qdbp.general.system.api.personnel.service.IUserCoreQueryer;

/**
 * 用户基础信息业务处理类
 *
 * @author zhh
 * @version 170215
 */
@Service
@Transactional(readOnly = true)
public class UserCoreQueryer implements IUserCoreQueryer {

    /** 用户基础信息DAO **/
    @Autowired
    private IUserCoreBasic userCoreBasic;

    @Override
    public UserCoreBean find(String id) throws ServiceException {
        return userCoreBasic.findById(id);
    }

    @Override
    public UserCoreBean find(UserCoreWhere where) throws ServiceException {
        return userCoreBasic.find(where);
    }

    @Override
    public PageList<UserCoreBean> list(UserCoreWhere where, OrderPaging paging) throws ServiceException {
        return userCoreBasic.list(where, paging);
    }

    @Override
    public List<UserCoreBean> listByIds(List<String> ids, IAccount... knowns) throws ServiceException {

        if (ids == null) {
            return null;
        }
        List<UserCoreBean> users = new ArrayList<>();
        if (ids.isEmpty()) {
            return users;
        }

        Map<String, UserCoreBean> map = new HashMap<>();
        if (knowns != null) {
            for (IAccount user : knowns) {
                if (user instanceof UserCoreBean.Wrap) {
                    UserCoreBean bean = ((UserCoreBean.Wrap) user).original();
                    map.put(bean.getId(), bean);
                }
            }
        }

        Set<String> temp = new HashSet<>();
        for (String id : ids) {
            if (map.containsKey(id)) {
                users.add(map.get(id));
            } else {
                temp.add(id);
            }
        }

        List<String> list = new ArrayList<>(temp);
        if (!list.isEmpty()) {
            UserCoreWhere where = new UserCoreWhere();
            where.setIds(list);
            PageList<UserCoreBean> PageList = userCoreBasic.list(where, OrderPaging.NONE);
            if (PageList != null) {
                users.addAll(PageList.getList());
            }
        }

        return users;
    }

    @Override
    public <T extends CreatorIdAware> List<UserCoreBean> listByCreatorIds(List<T> items, IAccount... knowns) throws ServiceException {

        if (items == null) {
            return null;
        }
        List<UserCoreBean> users = new ArrayList<>();
        if (items.isEmpty()) {
            return users;
        }

        Set<String> ids = new HashSet<>();
        for (T item : items) {
            ids.add(item.getCreatorId());
        }

        return listByIds(new ArrayList<>(ids), knowns);
    }

}